import request from '@/utils/request'
import form from '@/utils/form'
import { cached_data } from '@/utils/cache'

const api = 'partyUser/'

/**
 * 编辑用户党团
 *
 * @export
 * @param {*} { id, userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company,isRemoved,auth }
 * @return {*}
 */
export function updatePartyUser({ id, userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company, isRemoved, auth, allowOverwrite }) {
  return request.post(`${api}v2/info`, {
    data: { id, userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company, isRemoved },
    auth,
    allowOverwrite: false
  })
}
/**
 * 获取党团列表
 *
 * @export
 * @param {*} { group, duty, typeInParty, currentStep, company, page }
 * @return {*}
 */
export function getList({ group, duty, typeInParty, company, currentStep, page }) {
  return request.post(`${api}list`, getListPayloadBuild({ group, duty, typeInParty, company, currentStep, page }))
}
export function getListPayloadBuild({ group, duty, typeInParty, company, currentStep, page }) {
  return {
    group: form.toQueryValue(group),
    duty: form.toQueryStartEnd(duty),
    typeInParty: form.toQueryStartEndByArray(typeInParty),
    company: form.toQueryValue(company),
    currentStep: form.toQueryValue(currentStep),
    page
  }
}
/**
 * 提交党团
 *
 * @export
 * @param {*} { userName, dutyName, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company
 * }
 * @return {*}
 */
export function putItem({ userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company }) {
  return request.put(`${api}info`, {
    data: {
      userName,
      duty,
      dutyStart,
      companyAndDuty,
      time_Work,
      time_Party,
      typeInParty,
      partyGroup,
      create,
      company
    }
  })
}
/**
 * 编辑党团
 *
 * @export
 * @param {*} { id, userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company }
 * @return {*}
 */
export function patchItem({ id, userName, duty, dutyStart, companyAndDuty, time_Work, time_Party, typeInParty, partyGroup, create, company }) {
  return request.patch(`${api}info`, {
    data: {
      id,
      userName,
      duty,
      dutyStart,
      companyAndDuty,
      time_Work,
      time_Party,
      typeInParty,
      partyGroup,
      create,
      company
    }
  })
}

/**
 * 移除党团
 *
 * @export
 * @param {*} { userName }
 * @return {*}
 */
export function removeInfo({ userName }) {
  return request.delete(`${api}info`, {
    params: {
      userid: userName
    }
  })
}

const urlGetTypeInParty = `${api}typesInParty`
/**
 * 获取政治面貌列表
 *
 * @export
 */
export function typeInParty() {
  return cached_data(`${urlGetTypeInParty}`, () =>
    request.get(urlGetTypeInParty)
  )
}

const urlGetPartyDuties = `${api}partyDuties`
/**
 * 获取党内职务列表
 *
 * @export
 */
export function partyDuties() {
  return new Promise((res, rej) => {
    cached_data(`${urlGetPartyDuties}`, () =>
      request.get(urlGetPartyDuties)
    ).then(data => {
      // data.list.map(i => {
      //   i.value = i.name
      // })
      res(data)
    }).catch(e => rej(e))
  })
}
export default {
  getList,
  putItem,
  patchItem,
  removeInfo,
  typeInParty,
  partyDuties,
  updatePartyUser
}
